<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
  <style>
      body {
          background-color: #ffffff;
      }
  </style>
</head>
<body>
<div class="layui-form layuimini-form" id="myForm">
  <div class="layui-form-item">
    <label class="layui-form-label required" >登录名</label>
    <div class="layui-input-block">
      <input type="text" name="username" lay-verify="required" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required">姓名</label>
    <div class="layui-input-block">
      <input type="text" name="realName" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" >电话</label>
    <div class="layui-input-block">
      <input type="text" name="userTel"  lay-reqtext="不能为空" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required" >组织</label>
    <div class="layui-input-block">
      <select id="org" lay-verify="required" name="orgId">
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required" >角色</label>
    <div class="layui-input-block">
      <select id="role" lay-verify="required" name="roleId"></select>
    </div>
  </div>

  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label required" >密码</label>
    <div class="layui-input-block">
      <input type="text" name="basePassword" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入" value="" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">添加</button>
    </div>
  </div>
</div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../lib/jsencrypt/jsencrypt.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
  layui.use(['form'], function () {
    var form = layui.form,
      layer = layui.layer,
      $ = layui.$;

    $.ajax({
      type: 'POST',
      url: local + 'org/search/list',
      data: JSON.stringify({
        pageNum: 1,
        pageSize: 100
      }),
      dataType: "json",
      headers: {
        'Authorization': `Bearer ${ localStorage.getItem("token") }`,
      },
      contentType: 'application/json;charset=UTF-8',
      success: function (res) {
        if(res.code === 200){
          var list = res.data.records
          var select = document.getElementById('org');
          for (var i = 0; i < list.length; i++) {
            var option = document.createElement('option');
            option.value = list[i].orgId;
            option.text = list[i].orgName;
            select.appendChild(option);
          }
          form.render('select')
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
      }
    });


    $.ajax({
      type: 'POST',
      url: local + 'role/search',
      data: JSON.stringify({
        pageNum: 1,
        pageSize: 100
      }),
      dataType: "json",
      headers: {
        'Authorization': `Bearer ${ localStorage.getItem("token") }`,
      },
      contentType: 'application/json;charset=UTF-8',
      success: function (res) {//res为相应体,function为回调函数
        console.log('data', res)
        if(res.code === 200){
          // res.data.records
          var list = res.data.records
          var select = document.getElementById('role');
          for (var i = 0; i < list.length; i++) {
            var option = document.createElement('option');
            option.value = list[i].roleId;
            option.text = list[i].roleName;
            select.appendChild(option);
          }
          form.render('select');//重新渲染select组件，使动态添加的选项生效
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
      }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
      var obj = data.field
      var encrypt = new JSEncrypt();
      var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChIarNHyiuWuUhJVyZWnM1I4V8kKdjqW5qG+awNATynP6tq/dtj2zjhTo6yFpL4dYLXYVTHWJskxF68D/gga0+B12PUcR5QH4pobOFKD2xNhJiDxKdsP5zfTnEU39SJWXjeJJJNnGhpT+sz/Ng3e28TgKUaft5tAdcl32ioSx0YQIDAQAB";
      encrypt.setPublicKey(publicKey);

      obj.password = encrypt.encrypt(obj.basePassword)
      obj.roleId = obj.roleId.split(',')
      obj.userStatus = 0

      $.ajax({
        type: 'POST',
        url: local + 'user/insert',
        data: JSON.stringify(obj),
        dataType: "json",
        headers: {
          'Authorization': `Bearer ${ localStorage.getItem("token") }`,
        },
        contentType: 'application/json;charset=UTF-8',
        success: function (res) {//res为相应体,function为回调函数
          console.log('data', res)
          if(res.code === 200){
            layer.alert('添加成功');
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
            parent.layui.table.reload('currentTableId');
            return false;
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
        }
      });
    });
  });
</script>
</body>
</html>
